package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.roc.bean.TermBean;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.ModuleCategory;
import com.roc.pojo.Operators;
import com.roc.pojo.MerchantModuleRight;
import com.roc.pojo.Module;
import com.roc.pojo.OrgBean;
import com.roc.pojo.Termapply;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ITerminalService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.OperatorsStatus;
import com.roc.util.constant.SexStatus;

/**
 * 
 * @author 陈锴
 * 
 * 终端录入  /fmposm/terminal/queryTerminalApply.ct  
 * 
 * =================该方法已废除==========================
 * 
 * 2014-08-13
 *
 */

@Component
@Scope("prototype")
@Namespace("/terminal")
public class TerminalApplyAction extends PaginationBaseAction{
	
	private Logger logger=LogManager.getLogger(TerminalApplyAction.class);
	
	private TerminalApplySearchCondition searchCondition;
	
	private Termapply termapply;
	private String msg;
	
	private List<TermBean> termBeanList;
	
	@Autowired
	IorgService orgService;
	
	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired
	ITerminalService terminalService;
	
	
	@Action(value = "queryTerminalApply", results = { @Result(name = "success", location = "/pages/terminal/terminal_apply.jsp")
             })
	public String queryTerminalApplyAction() throws Exception{
		
		org();
		
		return SUCCESS;
	}
	
	@Action(value = "queryTerminalApplyList", results = { @Result(name = "success", location = "/pages/terminal/terminal_apply.jsp")
           })
	//查询终端申请
	public String queryTerminalApplyListAction() throws Exception{
		msg=null;
        Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		if(searchCondition==null){
			searchCondition=(TerminalApplySearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new TerminalApplySearchCondition();
			}
		}
		try {
			
			org();
			int rows=pagenitionService.countSQL(searchCondition);
			searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);

			List<Termapply> termapplyList= new ArrayList<Termapply>();
			if(searchRsList!=null&&searchRsList.size()>0){
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					Termapply term=new Termapply(); 
					term.setMerchantNo(obj[0].toString());
					term.setMerchantName(obj[1].toString());
					term.setOrgCode(obj[2].toString());
					term.setTermApplyed(Integer.parseInt(obj[3].toString()));
					term.setTermInstalled(Integer.parseInt(obj[4].toString()));
					term.setCreateDate(obj[5].toString());
					term.setOperators(obj[6].toString());
					term.setId(Long.parseLong(obj[7].toString()));
					String sta="";
					if(obj[8].toString().equals("W")){
						sta="待添加";
					}else{
						sta="添加完成";
					}
					term.setStatus(sta);
					termapplyList.add(term);
				}
			}
			getRequest().setAttribute("termapplyList",termapplyList);
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (ServiceException e) {
			
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			
		}
		
		return SUCCESS;
	}
	
	@Action(value = "queryTerminalApplyJsp", results = { @Result(name = "success", location = "/pages/terminal/terminal_save.jsp")
            })
	//跳转终端申请界面
	public String queryTerminalApplyJspAction(){
		
		try {
			termapply=terminalService.queryTerminal(termapply.getId());
			
			termapply.setCount(String.valueOf(termapply.getTermApplyed()-termapply.getTermInstalled()));
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return SUCCESS;
	}
	
	@Action(value = "queryTerminalApplySave", results = { @Result(name = "success", location = "/pages/terminal/terminal_apply.jsp")
             })
	//终端添加
	public String queryTerminalApplySaveAction() throws Exception{
		
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		try {
			terminalService.saveTerminal(termBeanList, logingMerchant.getAccountNo(),null);
			msg="添加终端成功";
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return queryTerminalApplyListAction();
	}
	
	//查询机构
	public void org() throws Exception{
		Operators logingOperators = (Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT);
		    List orgList=orgService.listOrgs(logingOperators);
		    getRequest().setAttribute("orgList", orgList);
			
	}
	
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (TerminalApplySearchCondition)getSession().getAttribute(SEARCH_SESSION_BEAN_TERMINAL_APPLY);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_TERMINAL_APPLY, searchCondition);
	}

	public TerminalApplySearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(TerminalApplySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	
	public Termapply getTermapply() {
		return termapply;
	}

	public void setTermapply(Termapply termapply) {
		this.termapply = termapply;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<TermBean> getTermBeanList() {
		return termBeanList;
	}

	public void setTermBeanList(List<TermBean> termBeanList) {
		this.termBeanList = termBeanList;
	}


}
